package com.szeldon.pj.jps.exp;

import com.szeldon.pj.jps.datastore.SimpleObject;
import com.szeldon.pj.jps.datastore.StringObject;

public class StringTerminalExpression extends TerminalExpression {

	private String value;

	/**
	 * 
	 * @param string
	 */
	public StringTerminalExpression(String string) {
		this.value = string;
	}

	@Override
	public String getValueAsString() {
		return value;
	}

	public String getValue() {
		return value;
	}

	@Override
	public int compareTo(SimpleObject object) throws IllegalStateException {
		if (!(object instanceof StringObject)) {
			throw new IllegalStateException("StringTerminalExpression can't be compared with object of"
					+ object.getClass().getName() + " type.");
		}

		String objectValue = ((StringObject) object).getValue();
		int comparisonValue = value.compareTo(objectValue);

		if (comparisonValue == 0) {
			return 0;
		} else if (comparisonValue < 0) {
			return -1;
		} else {
			return 1;
		}

	}

	@Override
	public int compareTo(TerminalExpression expression) {
		if (!(expression instanceof StringTerminalExpression)) {
			throw new IllegalStateException("StringTerminalExpression can't be compared with expression of"
					+ expression.getClass().getName() + " type.");
		}

		String objectValue = ((StringTerminalExpression) expression).getValue();
		int comparisonValue = value.compareTo(objectValue);

		if (comparisonValue == 0) {
			return 0;
		} else if (comparisonValue < 0) {
			return -1;
		} else {
			return 1;
		}
	}
}
